<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/12/30
 * Time: 11:00
 */

namespace coordinate;


class Gps
{
    public $PI = 3.14159265358979324;
    public $x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    
    public function delta($lat, $lon){
        // Krasovsky 1940
        //
        // a = 6378245.0, 1/f = 298.3
        // b = a * (1 - f)
        // ee = (a^2 - b^2) / a^2;
        $a = 6378245.0; //  a: 卫星椭球坐标投影到平面地图坐标系的投影因子。
        $ee = 0.00669342162296594323; //  ee: 椭球的偏心率。
        $dLat = $this->transformLat($lon - 105.0, $lat - 35.0);
        $dLon = $this->transformLon($lon - 105.0, $lat - 35.0);
        $radLat = $lat / 180.0 * $this->PI;
        $magic = sin($radLat);
        $magic = 1 - $ee * $magic * $magic;
        $sqrtMagic = sqrt($magic);
        $dLat = ($dLat * 180.0) / (($a * (1 - $ee)) / ($magic * $sqrtMagic) * $this->PI);
        $dLon = ($dLon * 180.0) / ($a / $sqrtMagic * cos($radLat) * $this->PI);
        return ['lat' => $dLat, 'lon' => $dLon ];
    }
    
    //WGS-84 to GCJ-02
    public function gcj_encrypt($wgsLat, $wgsLon){
        if ($this->outOfChina($wgsLat, $wgsLon)) return ['lat' => $wgsLat, 'lon' => $wgsLon];

        $d = $this->delta($wgsLat, $wgsLon);
        return ['lat' => $wgsLat + $d['lat'], 'lon' => $wgsLon + $d['lon']];
    }
    
    //GCJ-02 to WGS-84
    public function gcj_decrypt($gcjLat, $gcjLon){
        if ($this->outOfChina($gcjLat, $gcjLon)) return ['lat' => $gcjLat, 'lon' => $gcjLon];

        $d = $this->delta($gcjLat, $gcjLon);
        return ['lat' => $gcjLat - $d['lat'], 'lon' => $gcjLon - $d['lon']];
    }
    
    //GCJ-02 to WGS-84 exactly
    public function gcj_decrypt_exact($gcjLat, $gcjLon){
        $initDelta = 0.01;
        $threshold = 0.000000001;
        $dLat = $initDelta;
        $dLon = $initDelta;
        $mLat = $gcjLat - $dLat;
        $mLon = $gcjLon - $dLon;
        $pLat = $gcjLat + $dLat;
        $pLon = $gcjLon + $dLon;
        $wgsLat = 0;
        $wgsLon = 0;
        $i = 0;
        while (1) {
            $wgsLat = ($mLat + $pLat) / 2;
            $wgsLon = ($mLon + $pLon) / 2;
            $tmp = $this->gcj_encrypt($wgsLat, $wgsLon);
            $dLat = $tmp['lat'] - $gcjLat;
            $dLon = $tmp['lon'] - $gcjLon;
            if ((abs($dLat) < $threshold) && (abs($dLon) < $threshold))
                break;

            if ($dLat > 0){
                $pLat = $wgsLat;
            } else {
                $mLat = $wgsLat;
            }
            if ($dLon > 0){
                $pLon = $wgsLon;
            } else {
                $mLon = $wgsLon;
            }

            if (++$i > 10000) break;
        }
        //console.log(i);
        return ['lat' => $wgsLat, 'lon' => $wgsLon];
    }
    
    //GCJ-02 to BD-09
    public function bd_encrypt($gcjLat, $gcjLon){
        $x = $gcjLon;
        $y = $gcjLat;
        $z = sqrt($x * $x + $y * $y) + 0.00002 * sin($y * $this->x_pi);
        $theta = atan2($y, $x) + 0.000003 * cos($x * $this->x_pi);
        $bdLon = $z * cos($theta) + 0.0065;
        $bdLat = $z * sin($theta) + 0.006;
        return ['lat' => $bdLat, 'lon' => $bdLon];
    }
    
    //BD-09 to GCJ-02
    

    public function transformLat($x, $y) {
        $ret = -100.0 + 2.0 * $x + 3.0 * $y + 0.2 * $y * $y + 0.1 * $x * $y + 0.2 * sqrt(abs($x));
        $ret += (20.0 * sin(6.0 * $x * $this->PI) + 20.0 * sin(2.0 * $x * $this->PI)) * 2.0 / 3.0;
        $ret += (20.0 * sin($y * $this->PI) + 40.0 * sin($y / 3.0 * $this->PI)) * 2.0 / 3.0;
        $ret += (160.0 * sin($y / 12.0 * $this->PI) + 320 * sin($y * $this->PI / 30.0)) * 2.0 / 3.0;
        return $ret;
    }
    
    public function transformLon($x, $y){
        $ret = 300.0 + $x + 2.0 * $y + 0.1 * $x * $x + 0.1 * $x * $y + 0.1 * sqrt(abs($x));
        $ret += (20.0 * sin(6.0 * $x * $this->PI) + 20.0 * sin(2.0 * $x * $this->PI)) * 2.0 / 3.0;
        $ret += (20.0 * sin($x * $this->PI) + 40.0 * sin($x / 3.0 * $this->PI)) * 2.0 / 3.0;
        $ret += (150.0 * sin($x / 12.0 * $this->PI) + 300.0 * sin($x / 30.0 * $this->PI)) * 2.0 / 3.0;
        return $ret;
    }
}